package com.ybwh.springboot2.controller;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import com.ybwh.springboot2.common.RespData;
import com.ybwh.springboot2.dao.AreaMapper;
import com.ybwh.springboot2.model.po.Area;
import com.ybwh.springboot2.model.vo.AreaVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author saytime
 * @since 2018-12-06
 */
@NacosPropertySource(name = "", dataId = "my-config", autoRefreshed = true)
@Controller
@RequestMapping("/")
public class AreaController {



    String text;

    @Resource
    AreaMapper areaMapper;

    @ResponseBody
    @RequestMapping("/validate")
    public RespData<Object> validate(@Validated @ModelAttribute @RequestBody AreaVo areaVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            for (ObjectError error : bindingResult.getAllErrors()) {
                return RespData.failedResp(error.getDefaultMessage());
            }
        }
        Area area = areaMapper.selectById(110100L);

        return RespData.successResp(area);
    }

    @ResponseBody
    @RequestMapping("/get")
    public RespData<Object> get() {
        Area area = areaMapper.selectById(110100L);
        System.out.println("---------------------------" + text);

        return RespData.successResp(text);
    }


    @NacosValue("${server.xxxx}")
    public void setText(String text) {
        System.out.println("----222-----------------------" + text);
        this.text = text;
    }
}
